package net.yeah.chungchizhang.minecraft.mirrorsite.PaperMC;

import com.google.gson.Gson;
import net.yeah.chungchizhang.minecraft.mirrorsite.SingleThreadDownload;

import java.io.*;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PaperMC {
    public static final String Link = "https://papermc.io/api/v2/projects/";
    public void FetchProjects() {
        SingleThreadDownload Download = new SingleThreadDownload();
        TempFolder();
        Download.Download(Link, "./Temp/Paper/projects.json");
    }
    public void FetchProjectsInfo() throws Exception{
        SingleThreadDownload Download = new SingleThreadDownload();
        TempFolder();
        List<String> Projects = PharseProjects();
        for (String project : Projects) {
            Download.Download(Link + project, "./Temp/Paper/" + project + ".json");
        }
    }
    public void FetchVersionInfo() throws Exception{
        SingleThreadDownload Download = new SingleThreadDownload();
        TempFolder();
        List<String> Projects = PharseProjects();
        List<String> ProjectsInfo;
        for (String project : Projects) {
            ProjectsInfo = PharseProjectsInfo(project);
            for (String projectinfo : ProjectsInfo) {
                if (!new File("./Temp/Paper/" + project + "_" + projectinfo + ".json").exists()) {
                    Download.Download(Link + project + "/" + "versions/" + projectinfo, "./Temp/Paper/" + project + "_" + projectinfo + ".json");
                }
            }
        }
    }
    public void FetchJar() throws Exception {
        List<String> Projects = PharseProjects();
        List<String> ProjectsInfo;
        SingleThreadDownload Download = new SingleThreadDownload();
        ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*4);
        for(String project : Projects) {
            ProjectsInfo = PharseProjectsInfo(project);
            for (String projectinfo : ProjectsInfo) {
                if (!new File("./server/" + project).exists()) {new File("./server/" + project).mkdirs();}
                Download.URLTEMP = Link + project + "/versions/" + projectinfo + "/builds/" + PharseProjectsBuilds(project,projectinfo) + "/downloads/" + project + "-" + projectinfo + "-" + PharseProjectsBuilds(project,projectinfo) + ".jar";
                Download.FILENAMETMP = "./server/" + project + "/" + project + "_" + projectinfo + "_" + PharseProjectsBuilds(project, projectinfo) + ".jar";
                pool.submit(Download);
            }
        }
        pool.shutdown();
    }
    public void TempFolder() {
        File TempFolder = new File ("./Temp/Paper");
        if (!TempFolder.exists()) {
            TempFolder.mkdirs();
        }else {
            TempFolder.setWritable(true);
            TempFolder.setReadable(true);
        }
    }
    public List<String> PharseProjects() throws Exception {
        if (!new File("./Temp/Paper/projects.json").exists()) {
            FetchProjects();
        }
        Gson gson = new Gson();
        BufferedReader BR = new BufferedReader(new FileReader("./Temp/Paper/projects.json"));
        PharseProjects PP = gson.fromJson(BR, PharseProjects.class);
        return PP.getProjects();
    }
    public List<String> PharseProjectsInfo(String ProjectName) throws Exception {
        FetchProjectsInfo();
        Gson gson = new Gson();
        BufferedReader BR = new BufferedReader(new FileReader("./Temp/Paper/" + ProjectName + ".json"));
        PharseInfos PI = gson.fromJson(BR, PharseInfos.class);
        return PI.getVersions();
    }
    public String PharseProjectsBuilds(String ProjectName, String Version) throws Exception {
        FetchVersionInfo();
        Gson gson = new Gson();
        BufferedReader BR = new BufferedReader(new FileReader("./Temp/Paper/" + ProjectName + "_" + Version + ".json"));
        PharseBuilds PB = gson.fromJson(BR, PharseBuilds.class);
        return  PB.getLatestBuild();
    }
}
